package main.java.com.tool_stdy.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by yan_li on 2016/1/4.
 */

class CountNum {
    int i = 0;
    Lock lock = new ReentrantLock();
    public void add() {
        try {
            Thread.sleep(10);
            i++;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       // System.out.println(Thread.currentThread().getName());
    }

    public int getNum() {
        return i;
    }

}


public class TestThread {
   static  Lock lock = new ReentrantLock();
    public static void main(String[] args) throws Exception {
        final CountNum c = new CountNum();
        for(int i=0;i<3;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    lock.lock();
                    for(int i=0;i<10;i++){
                        c.add();
                    }
                    lock.unlock();
                }
            }).start();
        }
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    c.add();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    c.add();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    c.add();
                }
            }
        }).start();*/
        Thread.sleep(1000);
        System.out.println(c.getNum());
    }
}
